package cn.david.org.cotroller;

import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.PageList;
import cn.david.org.domain.Employee;
import cn.david.org.query.EmployeeQuery;
import cn.david.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
@CrossOrigin
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    @GetMapping
    @ApiOperation(value = "查询所有",notes = "需要传递ID")
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.add(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败！"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "需要传递ID")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败！"+e.getMessage());
        }

    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条",notes = "需要传递ID")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);

    }

    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);

    }

}
